package com.example.practiceopenglforyuv;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import android.Manifest;
import android.content.pm.PackageManager;
import android.graphics.ImageFormat;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.util.Log;
import android.util.Size;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    // Used to load the 'native-lib' library on application startup.
    static {
        System.loadLibrary("native-lib");
    }

    private static final String tag = "_" + "MainActivity";

    private MCamera2 mCamera2;
    private String mCameraId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        checkPermission();

        // for OpenGL
        GLSurfaceView glSurfaceView = findViewById(R.id.GLSurfaceView);
        glSurfaceView.setEGLContextClientVersion(2);
        MGLRenderer mglRenderer = new MGLRenderer(glSurfaceView);
        glSurfaceView.setRenderer(mglRenderer);
        glSurfaceView.setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);

        // for camera
        mCamera2 = new MCamera2(MainActivity.this, mglRenderer);
        mCameraId = "0";
        mCamera2.checkCameraParams();
        mCamera2.setCameraId(mCameraId);
        mCamera2.setCaptureParams(ImageFormat.YUV_420_888, new Size(1920, 1080));
        mCamera2.setPreviewParams(ImageFormat.YUV_420_888, new Size(1280, 720));
        mCamera2.startPreview();

        // for taking picture
        Button takeButton = findViewById(R.id.button);
        takeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mCamera2.takePicture();
            }
        });

        // for changing camera id
        Button changeButton = findViewById(R.id.button2);
        changeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mCamera2.stopPreview();
                if (mCameraId.equals("0")) {
                    mCameraId = "1";
                } else {
                    mCameraId = "0";
                }
                mCamera2.setCameraId(mCameraId);
                mCamera2.startPreview();
            }
        });

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mCamera2.unMCamera2();
    }

    private void checkPermission() {
        String[] permissions = new String[]{
                Manifest.permission.CAMERA,
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.READ_EXTERNAL_STORAGE
        };
        List<String> mPermissionList = new ArrayList<>();

        mPermissionList.clear();
        for (String permission1 : permissions) {
            if (ContextCompat.checkSelfPermission(MainActivity.this, permission1) != PackageManager.PERMISSION_GRANTED) {
                mPermissionList.add(permission1);
            }
        }
        if (mPermissionList.isEmpty()) {//未授予的权限为空，表示都授予了
            Toast.makeText(MainActivity.this, "已经授权", Toast.LENGTH_SHORT).show();
        } else {//请求权限方法
            String[] permission = mPermissionList.toArray(new String[0]);//将List转为数组
            ActivityCompat.requestPermissions(MainActivity.this, permission, 2);
        }
    }

    /**
     * A native method that is implemented by the 'native-lib' native library,
     * which is packaged with this application.
     */
    public native String stringFromJNI();
}
